# coding:utf-8

"""
CopyRight(c)  2017-09-15  Fraplus Corp

Author: Weilin Shen, Xiaodong.yang
Email:  xiaodong.yang@unidt.com
Functions:

we chat server

dependence package:

fralog
handler

"""
from fralog.FraLog import Fralog
import pymongo
# from globalsettings import *
from globalsettings import CUR_MODE
if CUR_MODE == 'dev':
    from globalsettings_dev import *
else:
    from globalsettings_prod import *
log = Fralog().getFileLog()


class Configue(object):
    host_src = MONGO_HOST
    port_src = MONGO_PORT

    host_dest = MONGO_HOST
    port_dest = MONGO_PORT

    def __init__(self):
        super(Configue).__init__()

    def migrate_data(self, dbsrc='survey_database', dbdest=None, collectsrc=None, collectdest=None):
        """
        从不同库、不同collectino之间迁移数据
        :param dbsrc: 
        :param dbdest: 
        :param collectsrc: 
        :param collectdest: 
        :return: 
        """
        client_src = pymongo.MongoClient(host=self.host_src, port=self.port_src)
        client_dest = pymongo.MongoClient(host=self.host_dest, port=self.port_dest)

        #
        # 默认数据库名相同
        if dbdest is None:
            dbdest = dbsrc

        if collectsrc is None:
            log.error('必须至指定源collection名')
            return

        if collectdest is None:
            collectdest = collectsrc

        db_src = client_src[dbsrc]
        db_dest = client_dest[dbdest]

        collect_src = db_src[collectsrc]
        collect_dest = db_dest[collectdest]

        rows = collect_src.find()
        collect_dest.remove()
        collect_dest.insert(rows)


if __name__ == '__main__':
    cf = Configue()
    cf.migrate_data(collectsrc='wxuser')
    log.info('用户数据迁移成功')
